package huizhi.service.flower;

import cn.hutool.core.util.StrUtil;
import org.bouncycastle.pqc.jcajce.provider.NTRU;

import java.sql.*;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 往数据库中插入数据
 */
public class InsertData {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        String id = "1111111111114545555";
        insertValue(id);
    }

    /**
     * 传入id值
     * @param id
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private static void insertValue(String id) throws SQLException, ClassNotFoundException {
        //获取连接对象
        Connection connection = ConnectionRequest.requestConnection();
        //获取statement对象
        Statement statement = connection.createStatement();

        //判断当前时间是星期几
        // 获取当前系统日期
        LocalDate currentDate = LocalDate.now();

        // 创建一个日期格式化器，指定格式为年-月-日
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 使用格式化器将日期转换为字符串
        //得到当前年月日的字符串
        String dateString = currentDate.format(formatter);
        System.out.println(dateString);
        // 获取当前日期是星期几
        DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
        int dayOfWeekValue = dayOfWeek.getValue();


        //写sql语句查询类别判断整箱还是混箱
        String sql2 = "select box_type from bis_flower_box where id = ? ";
        PreparedStatement pst = connection.prepareStatement(sql2);
        pst.setString(1,id);
        ResultSet resultSet1 = pst.executeQuery();
        String ZH = "";
        while (resultSet1.next()){
            if (resultSet1.getString(1).length()==0||resultSet1.getString(1).isBlank()){
                System.out.println("不存在");
                return;
            }
            if (resultSet1.getString(1).equals("1")){
                ZH = "ZX";
            }else {
                ZH = "HX";
            }
        }

        //查询当天的序号最大值
        List<Integer> numberHXList = new ArrayList<>();
        List<Integer> numberZXList = new ArrayList<>();
        List<String> numberStringListZX = new ArrayList<>();
        List<String> numberStringListHX = new ArrayList<>();
        int maxValue = 0;
        //查询整箱混箱的最大值
        if (ZH.equals("ZX")){
            String sqlDayNowZX = "select serial_number from bis_flower_box WHERE serial_number" +
                    " IS NOT NULL AND serial_number <> ' ' and DATE_FORMAT(create_time, '%Y-%m-%d')= '"+dateString+"' "+
                    " and box_type = 1";
            ResultSet resultSetZX = statement.executeQuery(sqlDayNowZX);
            while (resultSetZX.next()){
                numberZXList.add(Integer.parseInt(resultSetZX.getString(1).substring(6)));
                numberStringListZX.add(resultSetZX.getString(1));
            }
            if (numberZXList.size() == 0){
                maxValue = 0;
            }else {
                for (Integer i : numberZXList) {
                    if (i>maxValue){
                        maxValue = i;
                    }
                }
            }

        }else {
            String sqlDayNowHX = "select serial_number from bis_flower_box WHERE serial_number" +
                    " IS NOT NULL AND serial_number <> ' ' and DATE_FORMAT(create_time, '%Y-%m-%d')= '"+dateString+"' " +
                    " and box_type <> 1";
            ResultSet resultSetHX = statement.executeQuery(sqlDayNowHX);
            while (resultSetHX.next()){
                numberHXList.add(Integer.parseInt(resultSetHX.getString(1).substring(6)));
                numberStringListHX.add(resultSetHX.getString(1));
            }
            if (numberHXList.size() == 0){
                maxValue = 0;
            }else {
                for (Integer i : numberHXList) {
                    if (i>maxValue){
                        maxValue = i;
                    }
                }
            }
        }

        //编写数据
        String answerValue = "";
        answerValue += ZH;
        Random random = new Random();
        String answerValue2 = answerValue +"{}{}{}";
        String index = " ";
        //判断是否重复
        while (true){

//            int i1 = random.nextInt(900) + 100;

            // 创建一个包含所有字母的字符数组
            char[] allLetters = {'1', '2', '3', '4', '5', '6', '7', '8', '9','0','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

            // 生成三个随机索引
            int index1 = random.nextInt(allLetters.length);
            int index2 = random.nextInt(allLetters.length);
            int index3 = random.nextInt(allLetters.length);

            // 获取并打印三个随机字母
            char letter1 = allLetters[index1];
            char letter2 = allLetters[index2];
            char letter3 = allLetters[index3];
            String i1 = ""+letter1+letter2+letter3;

            index = StrUtil.format(answerValue2, i1, dayOfWeekValue, String.format("%04d", maxValue + 1));
            if (ZH.equals("ZX")){
                if (!xd(index,numberStringListZX)){
                    break;
                }
            }else if (ZH.equals("HX")){
                if (!xd(index,numberStringListHX)){
                    break;
                }
            }
        }
        System.out.println(index);
//        String sql3 = "UPDATE bis_flower_box set serial_number = ?  where id = ?;";
//        PreparedStatement preparedStatement = connection.prepareStatement(sql3);
//        preparedStatement.setString(1,index);
//        preparedStatement.setString(2,id);
//        int i = preparedStatement.executeUpdate();

    }

    /**
     * 判断a是否存在于集合内的方法
     * @param a
     * @param arrayList
     * @return
     */
    private static boolean xd(String a, List<String> arrayList) {
        for (String s : arrayList) {
            if (s.equals(a)){
                return true;
            }
        }
        return false;
    }
}
